﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace cubeworld.GUI
{
    /// <summary>
    /// Erstellt eine ProgressBar GUI
    /// </summary>
    public class ProgressBar
    {
        private int width, height, percent;
        private int border;
        private Vector2 position;

        /// <summary>
        /// Standard Konstruktor.
        /// </summary>
        /// <param name="width">Breite der ProgressBar.</param>
        /// <param name="height">Höhe der ProgressBar.</param>
        /// <param name="h2">Höhe des Fenster für die automatische Positionierung.</param>
        /// <param name="w2">Breite des Fenster für die automatische Positionierung.</param>
        public ProgressBar(int width, int height, int w2, int h2)
        {
            this.width = width;
            this.height = height;
            int x, y;
            x = (w2 / 2) - (width / 2);
            y = (h2 / 2) - (height / 2);
            this.position = new Vector2(x, y);
        }

        /// <summary>
        /// Standard Konstruktor mit eigener Positionierung.
        /// </summary>
        /// <param name="width">Breite der ProgressBar.</param>
        /// <param name="height">Höhe der ProgressBar.</param>
        /// <param name="x">Position von der X-Achse.</param>
        /// <param name="y">Position von der Y-Achse.</param>
        public ProgressBar(int width, int height, float x, float y)
        {
            this.width = width;
            this.height = height;
            this.position = new Vector2(x, y);
        }

        /// <summary>
        /// Standard Konstruktor mit benutzerdefinierten Prozent Wert.
        /// </summary>
        /// <param name="width">Breite der ProgressBar.</param>
        /// <param name="height">Höhe der ProgressBar.</param>
        /// <param name="percent">Start Prozent Wert.</param>
        /// <param name="h2">Höhe des Fenster für die automatische Positionierung.</param>
        /// <param name="w2">Breite des Fenster für die automatische Positionierung.</param>
        public ProgressBar(int width, int height, int percent, int w2, int h2)
        {
            this.width = width;
            this.height = height;
            this.setPercent(percent);
            int x, y;
            x = (w2 / 2) - (width / 2);
            y = (h2 / 2) - (height / 2);
            this.position = new Vector2(x, y);
        }

        private Texture2D generateTexture(GraphicsDevice gd)
        {
            Color[] data = new Color[width * height];
            //Background
            Color[] newData = new Color[width * height];
            int i = 0;
            foreach (Color c in data)
            {
                newData[i] = Color.Red;
                i++;
            }
            data = newData;
            //Border Top
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < border; y++)
                {
                    data[y * width + x] = Color.Black;
                }
            }
            //Border Bottom
            for (int x = 0; x < width; x++)
            {
                for (int y = height - border; y < height; y++)
                {
                    data[y * width + x] = Color.Black;
                }
            }
            //Border Left
            for (int x = 0; x < border; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    data[y * width + x] = Color.Black;
                }
            }
            //Border Rigth
            for (int x = width - border; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    data[y * width + x] = Color.Black;
                }
            }
            //Inhalt
            int minX = border;
            int maxX = border + (pixelPerPercent() * percent);
            for (int x = minX; x < maxX; x++)
            {
                for (int y = border; y < height - border; y++)
                {
                    data[y * width + x] = Color.DarkRed;
                }
            }
            Texture2D t = new Texture2D(gd, width, height);
            t.SetData<Color>(data);
            return t;
        }

        private int pixelPerPercent()
        {
            return (width - (border * 2)) / 100;
        }

        /// <summary>
        /// Zeichnet die ProgressBar.
        /// </summary>
        /// <param name="sb">SpriteBatch fürs Ausgeben von Text etc.</param>
        public void Render(SpriteBatch sb)
        {
            sb.Draw(generateTexture(Data.graphicsDevice), this.position, Color.White);
        }

        /// <summary>
        /// Setzt den Prozent Wert.
        /// </summary>
        /// <param name="i">Prozent Wert.</param>
        /// <exception cref="System.ArgumentException">
        /// Thrown any time the the Percent Value incorrect.
        /// </exception>
        public void setPercent(int i)
        {
            if (i > 100)
            {
                throw new ArgumentException("Argument \"i\" must be 100 or lower");
            }
            else if (i < 1)
            {
                throw new ArgumentException("Argument \"i\" must be 1 or higher");
            }
            this.percent = i;
        }

        /// <summary>
        /// Gibt den Prozent Wert zurück.
        /// </summary>
        /// <returns>Den Prozent Wert.</returns>
        public int getPercent()
        {
            return this.percent;
        }

        /// <summary>
        /// Gets or Sets the Border 
        /// </summary>
        public int Border
        {
            get { return this.border; }
            set { this.border = value; }
        }
    }
}
